//
//  NSArray+YTAdd.m
//  Elife
//
//  Created by 拓客营 on 2022/5/26.
//

#import "NSArray+YTAdd.h"
#import "NSObject+SwizzleMethod.h"

@implementation NSArray (YTAdd)

+(void)load{
    [super load];
    //无论怎样 都要保证方法只交换一次
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        //交换NSArray中的objectAtIndex方法
        [objc_getClass("__NSArrayI") SystemSelector:@selector(objectAtIndex:) swizzledSelector:@selector(sxy_objectAtIndex:)];
        //交换NSArray中的objectAtIndexedSubscript方法
        [objc_getClass("__NSArrayI") SystemSelector:@selector(objectAtIndexedSubscript:) swizzledSelector:@selector(sxy_objectAtIndexedSubscript:)];
    });
}

- (id)sxy_objectAtIndexedSubscript:(NSUInteger)idx{
    if (idx < self.count) {
        return [self sxy_objectAtIndexedSubscript:idx];
    }else{
        NSLog(@" 你的 NSArray数组已经越界了 但是已经帮你处理好了  %ld   %ld", idx, self.count);
        return nil;
    }
}

- (id)sxy_objectAtIndex:(NSUInteger)index{
    if (index < self.count) {
        return [self sxy_objectAtIndex:index];
    }else{
        NSLog(@" 你的 NSArray数组已经越界了 但是已经帮你处理好了  %ld   %ld", index, self.count);

        return nil;
    }
}

-(NSArray *)deleteNullString {
    
    NSMutableArray *arr = [NSMutableArray arrayWithArray:self];
    for (int i = (int)arr.count - 1; i >= 0; i--) {
        NSString *str = arr[i];
        if (![str isNotBlank]) {
            [arr removeObjectAtIndex:i];
        }
    }
    
    return [NSArray arrayWithArray:arr];
}

@end
